﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockImport.dataimport.tse.sb;
using StockCommon.data;
using System.IO;
using System.Collections;
using NetLib.web;
using Utilities;

namespace StockImport.dataimport.otc.sb
{
    //URL:  http://www.gretai.org.tw/ch/stock/aftertrading/broker_trading/brokerBS.php
    public class OTC_SB_Fetcher : SB_Fetcher_Common
    {
        protected override StockCommon.data.TwnStockMarketType getTargetMarket()
        {
            return TwnStockMarketType.上櫃;
        }

        protected override string getStoreFileName(string code, DateTime date)
        {
            String pat = @"D:\SBData\tempotc\{0}\{1}.csv";
            return String.Format(pat, DateUtils.toShortDateFormat_NoSlash(date), code);
        }

        public static String getURL()
        {
            return "http://www.gretai.org.tw/ch/stock/aftertrading/broker_trading/download_ALLCSV.php";
        }
        public void PerformTestFetch(String code)
        {
            Hashtable ps=new Hashtable();
            String url = "http://www.gretai.org.tw/ch/stock/aftertrading/broker_trading/brokerBS.php";
            ps.Add("Submit","確定");
            ps.Add("stk_code",code);

            Hashtable headerSet = new Hashtable();
            headerSet.Add("Referer", "http://www.gretai.org.tw/ch/stock/aftertrading/broker_trading/brokerBS.php");
            headerSet.Add("User-Agent", "	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1");
            headerSet.Add("Host", "www.gretai.org.tw");
            headerSet.Add("Accept-Language", "zh-tw,en-us;q=0.7,en;q=0.3");
            headerSet.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            String content = HttpPost.Request(url, ps, Encoding.GetEncoding(950), headerSet);

        }
        protected override bool CrawlingSB(string code, DateTime date, int tc)
        {
            String fn = getStoreFileName(code, date);
            if (File.Exists(fn))
                return true;
            String url = getURL();
            Hashtable ps = new Hashtable();
            String ds = String.Format("{0}{1:D2}{2:D2}", date.Year - 1911, date.Month, date.Day);
            ps.Add("fromw", "0");
            ps.Add("numbern", "100");
            ps.Add("curstk", code);
            ps.Add("stk_date", ds);

            Hashtable headerSet = new Hashtable();
            headerSet.Add("Referer", "http://www.gretai.org.tw/ch/stock/aftertrading/broker_trading/brokerBS.php");
            headerSet.Add("User-Agent", "	Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1");
            headerSet.Add("Host", "www.gretai.org.tw");
            headerSet.Add("Accept-Language", "zh-tw,en-us;q=0.7,en;q=0.3");
            headerSet.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

            bool success=false;
            String content = HttpPost.RequestCSV(url,ps,out success);
            if (success == false)
                return false;

            String pn = Path.GetDirectoryName(fn);
            if (Directory.Exists(pn) == false)
                Directory.CreateDirectory(pn);

            File.WriteAllText(fn, content, Encoding.GetEncoding("big5"));
            File.WriteAllText(fn + ".utf8.csv", content, Encoding.GetEncoding("utf-8"));
            return true;
        }
    }
}
